<?php
namespace app\index\controller;
use think\Db;
class Goods extends Base
{   
    /**
     * 搜索的产品结果
     */
    public function getlists(){
        if(is_post()){
            $page   = input('page') ? input('page') : 1;
            $cat_id = input('cat_id') ? input('cat_id') : "100all";
            $keyword= input('keyword');
            $orderby = input('orderby') ? input('orderby') : "sort";


            //原生态sql查询
            $sql = "
                select
                    [1]
                from
                    goods
                where
                    status = ?
                ";
            $param = [0];
            if ( $cat_id != '100all' ) {
                $g = new \app\admin\model\GoodsCat();
                $cat_ids = $g->getCatIds($cat_id);
                $cat_ids = implode(",", $cat_ids);
                $sql .= " and cat_id in ($cat_ids)";
            }
            if ( !empty($keyword) ) {
                $sql .= " and title like ? ";
                $param[] = "%{$keyword}%";
            }
            //总条数
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            //分页数据
            $page_size   = input('page_size') ? input('page_size') : c('goods_page_size');     #分页条数
            $page        = com_cur_page($total, $page,  $page_size);   #当前页
            $page_data   = com_page_data($total, $page,  $page_size);   #分页原始数据
            
            //列表数据
            $start       = ($page - 1) * $page_size;
            switch ($orderby){
                case "id":
                    $sql        .= " order by id desc limit $start, $page_size";
                    break;
                case "views":
                    $sql        .= " order by views desc, id desc limit $start, $page_size";
                    break;
                case "price":
                    $sql        .= " order by quanhou_price asc, id desc limit $start, $page_size";
                    break;
                case "coupon_num":
                    $sql        .= " order by coupon_num asc, id desc limit $start, $page_size";
                    break;
                default:
                    $sql        .= " order by sort desc, id desc limit $start, $page_size";
                    break;
            }
            $field       = " * ";
            $sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);
            //格式数据
            $str = "";
            foreach($list as $k => $v){
                $str .= '
						<li clear="clear">
							<a class="goods-a" href="'.url('goods/detail','id='.$v['id'].'&u='.$this->userinfo['id']).'" target="_self">
							<img src="'.$v['pic'].'" class="lazy" d-src="'.$v['pic'].'">
							</a>
							<a href="'.url('goods/detail','id='.$v['id'].'&u='.$this->userinfo['id']).'"  rel="nofollow">
							<h3>
							<b class="atitle">'.$v['title'].'</b>
							</h3>
							<div class="list-price buy clear">
							    <p>
							    <a href="'.url('goods/detail','id='.$v['id'].'&u='.$this->userinfo['id']).'" class="aquan">'.number_format($v['coupon_price']).'元优惠券</a>
							    </p>
								<p style="margin:5px 0px;" class="clear">
								<span class="span1">券后&nbsp;&nbsp;<b style="font-size:18px;">'.$v['quanhou_price'].'</b></span> 
								<span class="span2">已售'.$v['coupon_num'].'</span>
								</p>
								<p style="margin-bottom:5px;" class="clear">
									<a  href="'.url('goods/detail','id='.$v['id']).'&u='.$this->userinfo['id'].'" class="a1">领券购买</a>
									<a  href="javascript:;" tid="'.$v['id'].'" onclick="fenxiang(this)" class="a2">
								 	<span><img src="/static/jwap/images/share.png" class="img1">分享</span>
								 	</a>
								</p>
							</div>
							</a>
						</li>';
                
            }
            $com_body    = $str;
            $com_page    = com_build_page2( $page_data );
            $data = array(
                "com_body"  => $com_body,
                "com_page"  => $com_page,
            );
            com_check(['data'=>$data]);
        }
    }
    
    /**
     * 产品搜索页
     */
    public function slists( $keyword = '' ){
        $data = [
            'keyword' => $keyword,
        ];
        return view('', $data);
    }
    
    public function taokouling( $id ){
        return "<h1>稍后开放，请耐心等待....</h1>";
        $info = Db::name('goods')->find($id);
        $data = [
            'info' => $info,
        ];
        return view('',$data);
    }
    
    /**
     * 产品详情
     */
    public function detail( $id = 1)
    {   
        $orderby = ['sort','id','views', 'coupon_num'];
        $info = Db::name('goods')->find($id);
        $data = [
            'info' => $info,
            'orderby'      => $orderby[array_rand($orderby)],
            'is_fenxiang'  => cookie('?fenxiang'),
        ];
        return view('', $data);
    }

    public function fenxiang(){
        if(is_post()){
            $id = input('id');
            cookie('fenxiang', $id, ['expire' => 5]);
            com_check(['data'=>url('goods/detail', 'id='.$id.'&u='.$this->userinfo['id'])]);
        }
    }
}
